This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 440
/
NewTabPageP3AHelper.swift
269 lines (225 loc) · 8.88 KB
/
NewTabPageP3AHelper.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
import Foundation
import BraveCore
import Preferences
import OSLog
import Growth
protocol NewTabPageP3AHelperDataSource: AnyObject {
/// Whether or not Brave Rewards is enabled for the user.
///
/// When Rewards/Ads is enabled, the Ads library will handle reporting NTP SI events as usual
var isRewardsEnabled: Bool { get }
/// The associated tab's active URL.
///
/// Used to determine landing status of a clicked SI logo
var currentTabURL: URL? { get }
}
/// A P3A helper that will handle reporting dynamic P3A metrics around NTP SI interactions
///
/// A data source must be set in order to record events
final class NewTabPageP3AHelper {
private let p3aUtils: BraveP3AUtils
private var registrations: [P3ACallbackRegistration?] = []
weak var dataSource: NewTabPageP3AHelperDataSource?
init(p3aUtils: BraveP3AUtils) {
self.p3aUtils = p3aUtils
self.registrations.append(contentsOf: [
self.p3aUtils.registerRotationCallback { [weak self] type, isConstellation in
self?.rotated(type: type, isConstellation: isConstellation)
},
self.p3aUtils.registerMetricCycledCallback { [weak self] histogramName, isConstellation in
self?.metricCycled(histogramName: histogramName, isConstellation: isConstellation)
}
])
}
// MARK: - Record Events
private var landingTimer: Timer?
private var expectedLandingURL: URL?
/// Records an NTP SI event which will be used to generate dynamic P3A metrics
func recordEvent(
_ event: EventType,
on sponsoredImage: NTPSponsoredImageBackground
) {
assert(dataSource != nil, "You must set a data source to record events")
if !p3aUtils.isP3AEnabled || dataSource!.isRewardsEnabled == true {
return
}
let creativeInstanceId = sponsoredImage.creativeInstanceId
updateMetricCount(creativeInstanceId: creativeInstanceId, event: event)
if event == .tapped {
expectedLandingURL = sponsoredImage.logo.destinationURL
landingTimer?.invalidate()
landingTimer = Timer.scheduledTimer(withTimeInterval: 10, repeats: false) { [weak self] _ in
guard let self = self, let dataSource = self.dataSource else { return }
if let expectedURL = self.expectedLandingURL, expectedURL.isWebPage(),
dataSource.currentTabURL?.host == expectedURL.host {
self.recordEvent(.landed, on: sponsoredImage)
}
}
}
}
private func updateMetricCount(
creativeInstanceId: String,
event: EventType
) {
let name = DynamicHistogramName(creativeInstanceId: creativeInstanceId, eventType: event)
p3aUtils.registerDynamicMetric(name.histogramName, logType: .express)
var countsStorage = fetchEventsCountStorage()
var eventCounts = countsStorage.eventCounts[name.creativeInstanceId, default: .init()]
eventCounts.counts[name.eventType, default: 0] += 1
countsStorage.eventCounts[name.creativeInstanceId] = eventCounts
updateEventsCountStorage(countsStorage)
}
// MARK: - Storage
private func fetchEventsCountStorage() -> Storage {
guard let json = Preferences.NewTabPage.sponsoredImageEventCountJSON.value, !json.isEmpty else {
return .init()
}
do {
return try JSONDecoder().decode(Storage.self, from: Data(json.utf8))
} catch {
Logger.module.error("Failed to decode NTP SI Event storage: \(error)")
return .init()
}
}
private func updateEventsCountStorage(_ storage: Storage) {
do {
let json = String(data: try JSONEncoder().encode(storage), encoding: .utf8)
Preferences.NewTabPage.sponsoredImageEventCountJSON.value = json
} catch {
Logger.module.error("Failed to encode NTP SI Event storage: \(error)")
}
}
// MARK: - P3A Observers
private func rotated(type: P3AMetricLogType, isConstellation: Bool) {
if type != .express || isConstellation {
return
}
if !p3aUtils.isP3AEnabled {
Preferences.NewTabPage.sponsoredImageEventCountJSON.value = nil
return
}
let countBuckets: [Bucket] = [
0,
1,
2,
3,
.r(4...8),
.r(9...12),
.r(13...16),
.r(17...)
]
var countsStorage = fetchEventsCountStorage()
var totalActiveCreatives = 0
for (creativeInstanceId, eventCounts) in countsStorage.eventCounts {
for (eventType, count) in eventCounts.counts {
let name = DynamicHistogramName(creativeInstanceId: creativeInstanceId, eventType: eventType)
countsStorage.eventCounts[creativeInstanceId]?.inflightCounts[eventType] = count
UmaHistogramRecordValueToBucket(name.histogramName, buckets: countBuckets, value: count)
}
if !eventCounts.counts.isEmpty {
totalActiveCreatives += 1
}
}
updateEventsCountStorage(countsStorage)
let creativeTotalHistogramName = DynamicHistogramName(
creativeInstanceId: "total",
eventType: .init(rawValue: "count")
).histogramName
// Always send the creative total if ads are disabled (as per spec),
// or send the total if there were outstanding events sent
if dataSource?.isRewardsEnabled == false || totalActiveCreatives > 0 {
p3aUtils.registerDynamicMetric(creativeTotalHistogramName, logType: .express)
UmaHistogramRecordValueToBucket(creativeTotalHistogramName, buckets: countBuckets, value: totalActiveCreatives)
} else {
p3aUtils.removeDynamicMetric(creativeTotalHistogramName)
}
}
private func metricCycled(histogramName: String, isConstellation: Bool) {
if isConstellation {
// Monitor both STAR and JSON metric cycles once STAR is supported for express metrics
return
}
guard let name = DynamicHistogramName(computedHistogramName: histogramName) else {
return
}
var countsStorage = fetchEventsCountStorage()
guard var eventCounts = countsStorage.eventCounts[name.creativeInstanceId] else {
p3aUtils.removeDynamicMetric(histogramName)
return
}
let fullCount = eventCounts.counts[name.eventType] ?? 0
let inflightCount = eventCounts.inflightCounts[name.eventType] ?? 0
let newCount = fullCount - inflightCount
eventCounts.inflightCounts.removeValue(forKey: name.eventType)
if newCount > 0 {
eventCounts.counts[name.eventType] = newCount
} else {
p3aUtils.removeDynamicMetric(histogramName)
eventCounts.counts.removeValue(forKey: name.eventType)
if eventCounts.counts.isEmpty {
countsStorage.eventCounts.removeValue(forKey: name.creativeInstanceId)
}
}
if countsStorage.eventCounts[name.creativeInstanceId] != nil {
countsStorage.eventCounts[name.creativeInstanceId] = eventCounts
}
updateEventsCountStorage(countsStorage)
}
// MARK: -
struct EventType: RawRepresentable, Hashable, Codable {
var rawValue: String
static let viewed: Self = .init(rawValue: "views")
static let tapped: Self = .init(rawValue: "clicks")
static let landed: Self = .init(rawValue: "lands")
}
struct Storage: Codable {
typealias CreativeInstanceID = String
struct EventCounts: Codable {
var inflightCounts: [EventType: Int] = [:]
var counts: [EventType: Int] = [:]
}
var eventCounts: [CreativeInstanceID: EventCounts]
init(eventCounts: [CreativeInstanceID: EventCounts] = [:]) {
self.eventCounts = eventCounts
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.eventCounts)
}
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self.eventCounts = try container.decode([String: EventCounts].self)
}
}
private struct DynamicHistogramName: CustomStringConvertible {
var creativeInstanceId: String
var eventType: EventType
init(creativeInstanceId: String, eventType: EventType) {
self.creativeInstanceId = creativeInstanceId
self.eventType = eventType
}
init?(computedHistogramName: String) {
if !computedHistogramName.hasPrefix(P3ACreativeMetricPrefix) {
return nil
}
let items = computedHistogramName.split(separator: ".").map(String.init)
if items.count != 3 {
// The histogram name provided should be created from `histogramName` below
return nil
}
self.creativeInstanceId = items[1]
self.eventType = EventType(rawValue: items[2])
}
var histogramName: String {
// `P3ACreativeMetricPrefix` contains a trailing dot
return "\(P3ACreativeMetricPrefix)\(creativeInstanceId).\(eventType.rawValue)"
}
var description: String {
histogramName
}
}
}